package model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import applet.molecule.Atom;

/**
 * The molecule model. This is an abstract class that describe a molecule structure
 * in 3D space, or rather 3D locations of atoms.
 */
public abstract class MoleculeModel {
	
	/** molecule type enumeration. only water and hexanol is valid by now */
	public enum MoleculeType {
		WATER, HEXANOL
	};
	
	/** molecule type */
	protected MoleculeType type;
	
	/** 3D locations of atoms */
	protected List<Atom> list;
	
	/**
	 * Constructor of molecule model
	 */
	protected MoleculeModel(){
		list = new ArrayList<Atom>();
	}

	/**
	 * Parse configuration file and generate the molecule model.
	 * 
	 * @param reader the buffered reader accesses to configuration file.
	 * 
	 * @return molecule model
	 * 
	 * @throws IOException if there is some IO exception occur
	 */
	public static MoleculeModel parseMolecule(BufferedReader reader) throws IOException {
		MoleculeModel molecule = null;
		
		String line = reader.readLine();
		String[] split = line.trim().split("[\\s]+");
		if(split[1].equals("1")){
			molecule = WaterMolecule.parseWater(reader);
		}else if(split[1].equals("3")){
			molecule = HexanolMolecule.parseHexanol(reader);
		}
		
		return molecule;
	}

	/**
	 * Store the molecule model in file.
	 * 
	 * @param writer the PrintWriter accesses molecule data file.
	 */
	public void store(PrintWriter writer) {
		for(Atom ball: list){
			writer.print(ball.element + " ");
			writer.print(ball.x + " ");
			writer.print(ball.y + " ");
			writer.println(ball.z);
		}
	}
	
	/**
	 * Decide whether two models is similar.
	 * 
	 * @return true if they are similar
	 */
	public Float3D accumCoor(){
		Float3D f3D = new Float3D();
		for(Atom ball: list){
			f3D.x += ball.x;
			f3D.y += ball.y;
			f3D.z += ball.z;
		}
		
		return f3D;
	}
	
}
